package classrecord.classes.database.studenttelephonenumbertbl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.dbutils.DbUtils;

import classrecord.classes.database.common.IEveryTableDao;

public class STUDENTTELEPHONENUMBERTBLDao implements IEveryTableDao {

	private static final long serialVersionUID = 3350960012513591335L;

	private STUDENTTELEPHONENUMBERTBLDomain studenttelephonenumbertblDomain;
	private STUDENTTELEPHONENUMBERTBLDomain recordFoundDomain;

	private Connection connection;
	private PreparedStatement preparedStatement;
	private ResultSet resultSet;

	private final String RECORD_NUMBER = STUDENTTELEPHONENUMBERTBLColumn.RECORD_NUMBER.get();
	private final String STUDENT_NUMBER = STUDENTTELEPHONENUMBERTBLColumn.STUDENT_NUMBER.get();
	private final String TELEPHONE_NUMBER = STUDENTTELEPHONENUMBERTBLColumn.TELEPHONE_NUMBER.get();
	private final String IUD_FLAG = STUDENTTELEPHONENUMBERTBLColumn.IUD_FLAG.get();
	private final String CREATE_PID = STUDENTTELEPHONENUMBERTBLColumn.CREATE_PID.get();
	private final String CREATE_DATETIME = STUDENTTELEPHONENUMBERTBLColumn.CREATE_DATETIME.get();
	private final String DELETE_PID = STUDENTTELEPHONENUMBERTBLColumn.DELETE_PID.get();
	private final String DELETE_TIMESTAMP = STUDENTTELEPHONENUMBERTBLColumn.DELETE_TIMESTAMP.get();
	private final String UPDATE_PID = STUDENTTELEPHONENUMBERTBLColumn.UPDATE_PID.get();
	private final String UPDATE_TIMESTAMP = STUDENTTELEPHONENUMBERTBLColumn.UPDATE_TIMESTAMP.get();

	private final String NO_SPACE = "";

	public STUDENTTELEPHONENUMBERTBLDomain getStudenttelephonenumbertblDomain() {
		return studenttelephonenumbertblDomain;
	}

	public void setStudenttelephonenumbertblDomain(
			STUDENTTELEPHONENUMBERTBLDomain studenttelephonenumbertblDomain) {
		this.studenttelephonenumbertblDomain = studenttelephonenumbertblDomain;
	}

	public STUDENTTELEPHONENUMBERTBLDomain getRecordFoundDomain() {
		return recordFoundDomain;
	}

	public void setConnection(Connection connection) {
		this.connection = connection;
	}

	@Override
	public boolean add() {
		boolean wasInserted = false;
		String studentNumber = studenttelephonenumbertblDomain.getStudentNumber();
		
		String telephoneNumber = studenttelephonenumbertblDomain.getTelephoneNumber();
		if (null == telephoneNumber) { telephoneNumber = NO_SPACE; }
		
		String insertSql = "INSERT INTO STUDENTTELEPHONENUMBERTBL (STUDENT_NUMBER, TELEPHONE_NUMBER) VALUES (?, ?);";

		try {
			preparedStatement = connection.prepareStatement(insertSql);
			preparedStatement.setString(1, studentNumber);
			preparedStatement.setString(2, telephoneNumber);

			int recordInserted = preparedStatement.executeUpdate();
			wasInserted = recordInserted > 0;

		} catch (SQLException sqle) {
			sqle.printStackTrace();
		
		} finally {
			DbUtils.closeQuietly(preparedStatement);

		}

		return wasInserted;
	}

	@Override
	public boolean update() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean physicalDelete() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean logicalDelete() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int getPhysicalRecordCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getLogicalRecordCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * ***** Logically retrieve the details of a record. **********************
	 * [1]: This would retrieve the record of a corresponding row by asserting
	 * specific condition/s. [2]: The record's LOGICAL_DELETE_FLAG should not be
	 * equal to 1 for the details to be retrievable.
	 * **********************************************************************
	 */
	public STUDENTTELEPHONENUMBERTBLDomain logicalRetrieve() {
		return null;
	}

	/*
	 * ***** Physically retrieve the details of a record.**********************
	 * [1]: Only the administrator is advise to call this method. [2]: Even a
	 * logically–deleted record could be view once this method has been called.
	 * **********************************************************************
	 */
	public STUDENTTELEPHONENUMBERTBLDomain physicalRetrieve() {
		return null;
	}

}
